function CartUtils(options){
    var self =this;
    this.options={
        "maxItem":99,//购物车单件商品购买上限
        "minItem":1,//购物车单件商品购买下限
        "isCheckAll":false,
        "decreaseSelector":".weui-count__decrease",//增加商品数量按钮选择规则
        "increaseSelector":".weui-count__increase",//减少商品数量按钮选择规则
        "countNumSelector":".weui-count__number",//计数器选择规则
        "totalFeeSelector":"#totalFee",//总价显示选择规则
        "cartItemWrapper":".cart-item",//单个购物车商品的外包层选择规则
        "itemCheckBoxSelector":".cart-product-item input[type='checkbox']",//购物车单个物品按钮选择规则
        "totalFeeCheckBoxSelector":"#totalFeeCheckBox",//全选单个物品按钮选择规则
        "itemIdSelector":"#productIds",//选择购物车物件中的 ids 隐藏域选择规则
        "increaseEventCallBack":function(){//增加物品数量回掉事件

        },
        "decreaseEventCallBack":function(){//减少物品数量回掉事件

        },
        "caculateTotalFee":function(){
            var items = $(self.options.cartItemWrapper);
            var totalFee=0;
            $.each(items,function(index,item){
                var eachCheckBox = $(this).find(".weui-check");
                if(eachCheckBox.prop("checked")){
                    var eachCountNum = $(this).find(self.options.countNumSelector);
                    var itemPrice = eachCountNum.attr("data-item-price");
                    var number = eachCountNum.val();
                    var itemTotalFee = parseFloat(itemPrice)*number;
                    totalFee+=itemTotalFee;
                }
            })
            $(self.options.totalFeeSelector).html(totalFee);
        },//计算购物车中总价事件
        "chooseAllEvent":function(){
            var items = $(self.options.itemCheckBoxSelector);
            $(self.options.totalFeeCheckBoxSelector).prop("checked",true);
            $.each(items,function(index,item){
                $(this).prop("checked",true);
            });
        },//全选事件
        "cancelChooseAllEvent":function(){
            var items = $(self.options.itemCheckBoxSelector);
            $(self.options.totalFeeCheckBoxSelector).prop("checked",false);
            $.each(items,function(index,item){
                $(this).prop("checked",false);
            });
        },//取消全选事件
        "checkItemCheckBoxStatus":function(){
            var isCheckAll=true;
            var items = $(self.options.itemCheckBoxSelector);
            var productIds = new Array();
            $.each(items,function(index,item){
                if(!$(this).prop("checked")){
                    isCheckAll=false;
                    var temp = productIds.join(",");
                    $(self.options.itemIdSelector).val(temp);
                }else{
                    productIds.push($(this).val());
                    var temp = productIds.join(",");
                    $(self.options.itemIdSelector).val(temp);
                }
            });
            if(isCheckAll){
                $(self.options.totalFeeCheckBoxSelector).prop("checked",true)
            }else{
                $(self.options.totalFeeCheckBoxSelector).prop("checked",false)
            }
        }//单个购物车商品选择事件
    }
    this.options = Object.assign(this.options, options);//将设定参数和默认参数进行合并操作;
    CartUtils.prototype.initCart=function(){
        if(self.options.isCheckAll){
            self.options.chooseAllEvent();
        }else{
            self.options.cancelChooseAllEvent();
        }

        self.options.caculateTotalFee();
        //全选按钮处理事件
        $(self.options.totalFeeCheckBoxSelector).change(function(){
            var isCheck = $(this).prop("checked");
            if(isCheck){
                self.options.chooseAllEvent();
            }else{
                self.options.cancelChooseAllEvent()
            }
            self.options.checkItemCheckBoxStatus();
            self.options.caculateTotalFee();
        })
        //当个购物车中商品按钮处理事件
        $(self.options.itemCheckBoxSelector).change(function () {
            self.options.checkItemCheckBoxStatus();
            self.options.caculateTotalFee();
        })
       //减少商品数量处理事件
        $(self.options.decreaseSelector).click(function (e) {
            var $input = $(e.currentTarget).parent().find(self.options.countNumSelector);
            var number = parseInt($input.val() || "0") - 1
            if (number < self.options.minItem){
                number = self.options.minItem;
            }
            $input.val(number)
            self.options.decreaseEventCallBack();
            self.options.caculateTotalFee();
        });
        //增加商品数量处理事件
        $(self.options.increaseSelector).click(function (e) {
            var $input = $(e.currentTarget).parent().find(self.options.countNumSelector);
            var number = parseInt($input.val() || "0") + 1
            if (number > self.options.maxItem){
                number = self.options.maxItem;
            }
            $input.val(number)
            self.options.increaseEventCallBack();
            self.options.caculateTotalFee();
        });
    }
}

